from pydantic import BaseModel, Field, field_validator


class UpdateScript(BaseModel):
    filename: str = Field(..., min_length=1, max_length=16, description="文件名")


class StartScriptRequest(BaseModel):
    filename: str = Field(..., min_length=1, max_length=16, description="脚本名称")
    users: int = Field(..., gt=0, description="并发用户数")
    spawn_rate: int = Field(..., gt=0, description="每秒生成用户数")
    run_time: str = Field(..., max_length=6, description="运行时间，如10s/1m/1h")
    host: str = Field(..., description="被压测系统地址")

    @classmethod
    @field_validator("run_time")
    def validate_run_time(cls, v: str):
        if not any(v.endswith(suffix) for suffix in ("s", "m", "h")):
            raise ValueError("运行时间必须以 s、m 或 h 结尾")
        return v
